package io.renren.modules.app.service.impl;

import io.renren.modules.app.dao.FslipDao;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.entity.FslipEntity;
import io.renren.modules.app.entity.ImGroupEntity;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.FslipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;


@Service("fslipService")
public class FslipServiceImpl extends ServiceImpl<FslipDao, FslipEntity> implements FslipService {

    @Autowired
    private FslipDao fslipDao;
    @Autowired
    private UserDao userDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FslipEntity> page = this.page(
                new Query<FslipEntity>().getPage(params),
                new QueryWrapper<FslipEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<FslipEntity> getFslipList(FslipEntity fslfip) {

        return fslipDao.getFslipList(fslfip);
    }

    @Override
    @Transactional
    public  FslipEntity addFslip(FslipEntity fslfip) {
        UserEntity user = userDao.selectOne(new QueryWrapper<UserEntity>().eq("username", fslfip.getUsername()));
        fslfip.setSenderid(user.getUserId());
        fslfip.setCreateTime(new Date());
        fslfip.setUsername(null);
        this.save(fslfip);
        return fslipDao.selectOneByid(fslfip);
    }

    @Override
    public List<ImGroupEntity> getUserFriends(String userId) {
        return this.baseMapper.getFslipList(userId);
    }

}
